// (c) Copyright 2013 Stephen Gold <sgold@sonic.net>
// Distributed under the terms of the GNU General Public License

/*
 This file is part of the Bats Game.

 The Bats Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by the
 Free Software Foundation, either version 3 of the License, or (at your
 option) any later version.

 The Bats Game is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.

 You should have received a copy of the GNU General Public License
 along with the Bats Game.  If not, see <http://www.gnu.org/licenses/>.
 */
package bats.ui;

import java.util.Collection;
import java.util.logging.Logger;
import jme3utilities.MyString;
import jme3utilities.ui.InputMode;

/**
 * An item in the action listbox of the hotkey binding screen.
 *
 * @author Stephen Gold <sgold@sonic.net>
 */
class ActionItem {
    // *************************************************************************
    // constants

    /**
     * message logger for this class
     */
    final private static Logger logger =
            Logger.getLogger(ActionItem.class.getName());
    // *************************************************************************
    // fields
    /**
     * which input mode (not null, set by constructor)
     */
    final private InputMode mode;
    /**
     * the action name (not null, set by constructor)
     */
    final private String actionName;
    // *************************************************************************
    // constructors

    /**
     * Instantiate an item from an action name and an input mode.
     *
     * @param actionName (not null)
     * @param mode (not null)
     */
    ActionItem(String actionName, InputMode mode) {
        assert actionName != null;
        assert mode != null;

        this.actionName = actionName;
        this.mode = mode;
    }
    // *************************************************************************
    // new methods exposed

    /**
     * Access the action name.
     */
    String getActionName() {
        return actionName;
    }
    // *************************************************************************
    // Object methods

    /**
     * Convert the item to text for listbox display.
     *
     * @return display text for the item
     */
    @Override
    public String toString() {
        String result = String.format("  %s", MyString.quote(actionName));
        Collection<String> names = mode.findHotkeys(actionName);
        if (!names.isEmpty()) {
            result += String.format(":     %s", formatList(names));
        }
        return result;
    }
    // *************************************************************************
    // private methods

    /**
     * Format a collection of hotkey names for display.
     *
     * @param names (not null)
     */
    private static String formatList(Collection<String> names) {
        StringBuilder result = new StringBuilder();
        boolean firstFlag = true;
        for (String keyName : names) {
            if (!firstFlag) {
                result.append(" and ");
            }
            result.append(String.format("[ %s ]", keyName));
            firstFlag = false;
        }
        return result.toString();
    }
}